window.onload = function() {
    
    $('#upload_img_icon').click(function (e) { 
        $('#upload_img').click()
        
    });

    $("#hide").hide()
    // let username = localStorage.getItem("username");
    
    
    let username = GetQueryString("username")
    let username2 = getQueryVariable("username")
    $("#user").text("你好" + username)
    console.log(username)
    // $.ajax({
    //     type: "post",
    //     url: "http://localhost:8080/getUser.do",
    //     data: {"username":username},
    //     dataType: "json",
    //     success: function (resp) {
    //         console.log("username从数据库")
    //         console.log(resp)
    //         $("#hide").val(resp.username)
    //     }
    // });
    $("#hide").text(username)
    function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
    
    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    console.log('r')
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/findAllArticles.do",
        dataType: "json",
        success: function (response) {
            let orlHtml = $("#articleContainer").html();
            console.log(orlHtml)
            console.log(response)
            console.log()
            for (let i = 0; i < response.length; i++) {
                const element = response[i];
                console.log(element)
                orlHtml += `
                <div class="article">
                    <div class="content">
                        <div class="title">${element.content}</div>
                        <div class="auth">
                            <span>作者：${element.username}</span>
                            <span>时间：${element.date}</span>
                        </div>
                    </div>
                    <div class="img">
                        <img src="/uploads/${element.reFilename}" alt="">
                    </div>
                </div>
                `
            }
            $("#articleContainer").html(orlHtml)
            
        },
        error: function(err){
            console.log(err)
        }
        

        
    });


}